import got from "got";
import { readFileByType } from "./util";

const prefixUrl = process.env.ICAN_PREFIXURL;
const postThoughtsUrl = process.env.ICAN_POST_THOUGHTS;
const token = readFileByType('access_token')

class ThoughtsDTO {
  content = ""
  images: any[] = []
  status = 1
  tags = []
}


const postThoughts = async (id: string) => {
  const newsList: any[] = readFileByType('cctv.news.list', 'json');
  const thought = newsList.find(m => m.id === id);
  if (!thought) { return console.error("没有找到该新闻"); }
  const headers = {
    authorization: `Bearer ${token}`
  }
  const postUrl = `${prefixUrl}${postThoughtsUrl}`;
  const thoughtsDto = new ThoughtsDTO()
  thoughtsDto.content = thought.brief;
  thoughtsDto.images = thought.image && [thought.image] || [];
  thoughtsDto.tags = thought.keywords.split(" ");
  try {
    const res = await got.post(postUrl, {
      method: "POST",
      headers,
      json: thoughtsDto
    });
    console.log('发说说成功!');
    return res;
  } catch (error) {
    console.error(error);
  }
}

export { postThoughts }